home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d20
/
enet_201.arc
/
EXTR_NET.C
next >
Wrap
C/C++ Source or Header
|
1992-01-15
|
16KB
|
516 lines
#define TITLE "EXTR_NET" /* Program Name */
#define DATES "1988-91" /* Copyright Date */
#define VERSION "2.01" /* Version Number */
/*
EXTR_NET
Version 2.01
Copyright (c) Bob Swift, 1988-91. All Rights Reserved.
Compiled with the Turbo C++ 1.0 compiler from Borland.
This program is designed to read an input nodelist file and extract
selected Networks, Regions and/or Zones. The extracted portions of
the nodelist are stored in a file specified on the command line.
The format for using this program is as follows:
EXTR_NET <srcfile> <destfile> <net1> <net2>...<net10>
where <srcfile> is the name of the nodelist file to be read
<destfile> is the name of the output file created
<net..> are the numbers of the Net/Region/Zones to be extracted
(the program is presently set to handle 10 numbers).
These numbers may be preceded by a N, R or Z in order
to extract the entire Net, Region or Zone. Examples
are: 123 N140 r17 Z1
The nodelist may be specified with a full drive and path. The destination
file must not have a drive or path specified. It will be output to the
current directory.
The program will display a VERY brief set of instructions if it is called
without any arguments or if it encounters an error. The following is a
list of the error codes returned by the program:
0 - No errors. Normal termination.
1 - Bad or missing command line argument.
2 - Unable to open the input file.
3 - Unable to open the output file.
4 - Problem writing to output file.
5 - Problem closing input file.
6 - Problem closing output file.
7 - Unable to open configuration file.
8 - Unable to create backup file.
When an error is encountered, the program will exit immediately and will
attempt to properly close all files.
Although I have chosen to retain all rights to this program, you are free
to use it under the following conditions:
- You realize that there is NO Warrantee of any sort.
It was tested pretty thoroughly here before release
but who knows what bugs may be lurking within.
- You will not modify the code and release a new version
of the program. I welcome suggestions for improvement
(especially when accompanied by code) but I make no
guarantee of future releases.
- If you find the program useful, I ask that you do
something to brighten somebody else's day. Just
exactly what, I will leave up to you.
You may freely distribute this program provided that you distribute only
the complete archive which includes the EXTR_NET.EXE, EXTR_NET.C and
EXTR_NET.DOC. In addition, You MUST NOT charge for the program nor can
you charge a copy fee over $4.00 (including the price of the diskette).
Bob Swift (1:140/24)
Revision History
----------------
1.00 88/11/08 First release version.
1.01 88/11/08 Correct a minor problem with file names and the
creation of the output backup file.
1.10 88/11/09 Finally corrected the null pointer and backup bug
for good this time. Someday I will learn to handle
strings properly <grin>. Also cleaned up the error
code handling section so that the error messages
make sense and included comment lines in output
file to show what input file was used and to
separate each extracted Net/Region.
2.00 89/05/07 Modified command line inputs slightly to allow
extraction of entire Net, Region or Zone by specifying
N, R or Z with the number. Numbers entered with no
specifier will be matched to anything but will only
extract until the next occurence of a Host, Region or
Zone statement. Also echoed the search criteria to
the output file.
2.01 91/12/28 Cleaned up comments in output file.
*/
#include <stdio.h>
#include <string.h>
void helpscrn(int ernum);
void add_period(char filename[]);
void build_bak_name(char filename[]);
int is_in_dir(char *filename);
int del_backup(char *filename);
int linetest(char *linetst1, char *inline);
int make_backup(char *srcfile, char *destfile);
char version[] = {VERSION}; /* Current Version Number */
char title[] = {TITLE};
char dates[] = {DATES};
/*******************************************************************/
void main(int argc, char *argv[])
{
FILE *infile,*outfile;
char flnames[3][40];
char inline[358];
char linetst1[20];
char lintest[31][20];
char linshow[21][20];
int lineflag[31];
char semi[20];
char spaces[80];
int i,j;
int sub1;
int count;
int flag;
strcpy(spaces," ");
printf("\n\n%s Version %s\n",title,version);
printf("Copyright (c) Bob Swift, %s. All Rights Reserved.\n",dates);
if (argc < 4 || argc > 14) helpscrn(1);
strcpy(flnames[1],strupr(argv[1])); /* Input File */
strcpy(flnames[2],strupr(argv[2])); /* Output File */
strcpy(flnames[3],strupr(argv[2])); /* Backup File */
if (is_in_dir(flnames[2]) == 1)
helpscrn(7);
add_period(flnames[3]);
build_bak_name(flnames[3]);
if (del_backup(flnames[3]) == 1)
helpscrn(8);
/* Exit if output file has an extension of BAK */
if (strcmp(flnames[2],flnames[3])==0)
helpscrn(8);
if (make_backup(flnames[2],flnames[3]) == 1)
helpscrn(8);
infile=fopen(flnames[1],"rt");
if (infile == NULL)
helpscrn(2);
if ((outfile=fopen(flnames[2],"wt")) == NULL)
helpscrn(3);
count = 3;
sub1 = 0;
while (count < argc) /* Set Up Search Strings */
{
strcpy(semi,argv[count]);
switch(semi[0])
{
case 'n': ;
case 'N': sub1++;
strcpy(lintest[sub1],"Host,");
strcpy(linshow[sub1],"Net ");
lineflag[sub1] = 'N';
i = 5;
j = 1;
while (semi[j] != '\0')
{
lintest[sub1][i] = semi[j];
i++;
j++;
}
lintest[sub1][i] = '\0';
strcat(lintest[sub1],",");
i = 4;
j = 1;
while (semi[j] != '\0')
{
linshow[sub1][i] = semi[j];
i++;
j++;
}
linshow[sub1][i] = '\0';
break;
case 'r': ;
case 'R': sub1++;
strcpy(lintest[sub1],"Region,");
strcpy(linshow[sub1],"Region ");
lineflag[sub1] = 'R';
i = 7;
j = 1;
while (semi[j] != '\0')
{
lintest[sub1][i] = semi[j];
linshow[sub1][i] = semi[j];
i++;
j++;
}
lintest[sub1][i] = '\0';
linshow[sub1][i] = '\0';
strcat(lintest[sub1],",");
break;
case 'z': ;
case 'Z': sub1++;
strcpy(lintest[sub1],"Zone,");
strcpy(linshow[sub1],"Zone ");
lineflag[sub1] = 'Z';
i = 5;
j = 1;
while (semi[j] != '\0')
{
lintest[sub1][i] = semi[j];
linshow[sub1][i] = semi[j];
i++;
j++;
}
lintest[sub1][i] = '\0';
linshow[sub1][i] = '\0';
strcat(lintest[sub1],",");
break;
default:
{
sub1++;
lineflag[sub1] = 'A';
strcpy(lintest[sub1],"Zone,");
strcpy(linshow[sub1],"Zone ");
strcat(lintest[sub1],argv[count]);
strcat(linshow[sub1],argv[count]);
strcat(lintest[sub1],",");
sub1++;
lineflag[sub1] = 'A';
strcpy(lintest[sub1],"Region,");
strcpy(linshow[sub1],"Region ");
strcat(lintest[sub1],argv[count]);
strcat(linshow[sub1],argv[count]);
strcat(lintest[sub1],",");
sub1++;
lineflag[sub1] = 'A';
strcpy(lintest[sub1],"Host,");
strcpy(linshow[sub1],"Net ");
strcat(lintest[sub1],argv[count]);
strcat(linshow[sub1],argv[count]);
strcat(lintest[sub1],",");
}
}
count++;
}
printf("Extracting: "); /* Echo Input To User */
for (count=1;count<=sub1;count++)
{
printf("%s\n ",linshow[count]);
}
printf("\n");
/* Print header to output file */
if (fputs(";A Extracted Nodelist Segment : 0\n",outfile) == EOF) helpscrn(4);
if (fputs(";A \n;A Extracted with ",outfile) == EOF) helpscrn(4);
if (fputs(title,outfile) == EOF) helpscrn(4);
if (fputs(" Version ",outfile) == EOF) helpscrn(4);
if (fputs(version,outfile) == EOF) helpscrn(4);
if (fputs("\n;A ",outfile) == EOF) helpscrn(4);
if (fputs("Copyright (c) Bob Swift, ",outfile) == EOF) helpscrn(4);
if (fputs(dates,outfile) == EOF) helpscrn(4);
if (fputs(". All Rights Reserved.",outfile) == EOF) helpscrn(4);
if (fputs("\n;A \n;A Extracted from ",outfile) == EOF) helpscrn(4);
if (fputs(flnames[1],outfile) == EOF) helpscrn(4);
if (fputs("\n;A \n;A Extracting:\n;A ",outfile) == EOF) helpscrn(4);
for (count=1;count<=sub1;count++)
{
if(fputs("\n;A ",outfile) == EOF) helpscrn(4);
if(fputs(linshow[count],outfile) == EOF) helpscrn(4);
}
if (fputs("\n;A \n;\n",outfile) == EOF) helpscrn(4);
/* Initialize */
count = sub1;
flag = 1;
j = 0;
/* Get line from source file */
while (fgets(inline,256,infile) != NULL)
{
strcpy(semi,";");
i = linetest(semi,inline);
strcpy(semi,";\n");
if ((linetest(semi,inline) == 1 && flag == 0) || i == 0)
{
if (flag == 0)
{
strcpy(semi,"Host");
i = linetest(semi,inline);
if (i == 1 && (j == 'N' || j == 'A')) flag = 1;
if (flag == 1) if (fputs(";\n",outfile) == EOF) helpscrn(4);
}
if (flag == 0)
{
strcpy(semi,"Region");
i = linetest(semi,inline);
if (i == 1 && (j == 'N' || j == 'R' || j == 'A')) flag = 1;
if (flag == 1) if (fputs(";\n",outfile) == EOF) helpscrn(4);
}
if (flag == 0)
{
strcpy(semi,"Zone");
flag = linetest(semi,inline);
if (flag == 1) if (fputs(";\n",outfile) == EOF) helpscrn(4);
}
if (flag == 0)
{
if (fputs(inline,outfile) == EOF)
helpscrn(4);
}
if (flag == 1)
{
/* Cycle through the search patterns */
for (sub1=1;sub1<=count && linetest(lintest[sub1],inline)==0;sub1++)
{
/* Null Line */
}
if (sub1 <= count)
{
if (fputs(inline,outfile) == EOF)
helpscrn(4);
flag = 0;
j = lineflag[sub1];
}
}
}
if (inline[0] == 'Z' || inline[0] == 'R' || (inline[0] == 'H' && inline[2] == 's'))
{
inline[strlen(inline)-1] = '\0';
i = 0;
for (sub1=0;sub1<=strlen(inline) && i<3;sub1++)
{
if (inline[sub1] == ',')
{
i++;
if (i >= 3)
inline[sub1] = '\0';
if (i == 1)
inline[sub1] = ' ';
}
if (inline[sub1] == '_')
inline[sub1] = ' ';
}
strcat(inline,spaces);
inline[79] = '\0';
printf("%s",inline);
if (inline[0] == 'Z')
printf("\n");
else
printf("\r");
}
}
if (fputs(";S End of extracted file.\n;S \n",outfile) == EOF) helpscrn(4);
/* Close the source file */
if (fclose(infile) != 0)
helpscrn(5);
/* Close the destination file */
if (fclose(outfile) != 0)
helpscrn(6);
/* Thank the user and exit gracefully */
printf("%s",spaces);
printf("\nExtraction complete. Thank-you for using %s.\n\n",title);
exit(0);
}
/*******************************************************************/
int is_in_dir(char *filename)
/* Check if input file in current directory */
{
if (strchr(filename,'\\')!=NULL || strchr(filename,':')!=NULL)
return(1);
else
return(0);
}
void add_period(char filename[])
/* If input file has no extension finish it with a period */
{
if (strchr(filename,'.')==NULL)
strcat(filename,".");
}
void build_bak_name(char filename[])
/* Build file name for backup file */
{
int flag;
for (flag=0;filename[flag]!='.';flag++)
{
/* NULL Line */
}
filename[flag] = '\0';
strcat(filename,".BAK");
}
int del_backup(char *filename)
/* Check for existing backup file and delete */
{
FILE *tempfile;
tempfile=fopen(filename,"r");
fclose(tempfile);
if (tempfile != NULL)
{
if (unlink(filename) != 0)
return(1);
}
return(0);
}
int make_backup(char *srcfile, char *destfile)
/* Rename source file to destination file */
{
FILE *tempfile;
tempfile=fopen(srcfile,"r");
fclose(tempfile);
if (tempfile != NULL)
{
if (rename(srcfile,destfile) != 0)
return(1);
}
return(0);
}
int linetest(char *linetst1, char *inline)
/* This is the routine that actually checks for a match */
{
int j;
for ( j=0; linetst1[j] == inline[j]; j++ )
if (linetst1[j+1] == '\0') return(1);
return(0);
}
void helpscrn(int ernum)
/* Here are the error messages and VERY brief instructions */
{
cprintf("\n");
switch (ernum) {
case 1 : printf("Bad or missing command line argument\n\n");
break;
case 2 : printf("Unable to open input file\n\n");
break;
case 3 : printf("Unable to open output file\n\n");
break;
case 4 : printf("Problem writing to output file\n\n");
break;
case 5 : printf("Problem closing input file\n\n");
break;
case 6 : printf("Problem closing output file\n\n");
break;
case 7 : printf("Output file not in current directory\n\n");
break;
case 8 : printf("Unable to create backup file\n\n");
break;
}
printf("This program is designed to read an input nodelist file and extract");
printf("\nselected Networks, Regions and/or Zones. The extracted portions ");
printf("of\nthe nodelist are stored in a file specified on the command line");
printf(".\n\nThe format for using this program is as follows:\n\n\n");
printf(" EXTR_NET <srcfile> <destfile> <net1> <net2>...<net10>\n\n");
printf("where <srcfile> is the name of the nodelist file to be read\n");
printf(" <destfile> is the name of the output file created\n");
printf(" <net..> are the numbers of the Net/Region/Zones to be ex");
printf("tracted\n (the program is presently set to handle");
printf(" 10 numbers).\n These numbers may be preceded by ");
printf("a N, R or Z in order\n to extract the entire Net,");
printf(" Region or Zone. Examples\n are: 123 N140 r17 Z1");
printf("\n\n");
exit(ernum);
}